package net.bxx2004.extand.events;

import net.bxx2004.extand.manager.ConfigManager;
import net.bxx2004.extand.util.JDBCUtil;
import net.bxx2004.extand.values.Value;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MenuClick implements Listener {
    @EventHandler
    public void onClick(InventoryClickEvent event){
        try {
            if (event.getView().getTitle().equalsIgnoreCase(ConfigManager.getSetting().getString("Gui.Title")) && event.getInventory().getSize() == ConfigManager.getSetting().getInt("Gui.Size")){
                event.setCancelled(true);
                Player player = (Player) event.getWhoClicked();
                if (event.getCurrentItem().getItemMeta().getLore().contains(ConfigManager.getSetting().getString("InPutCodeLore"))){
                    switch (ConfigManager.getSetting().getString("Storge.Type")){
                        case "Yaml":
                            a(player);
                            break;
                        case "MySql":
                            Connection connection = JDBCUtil.getConnection();
                            String sql = "select * from " + ConfigManager.getSetting().getString("Storge.SqlTablename") + " where PlayerName='"+ player.getName() +"'";
                            PreparedStatement pst = connection.prepareStatement(sql);
                            ResultSet rs  = pst.executeQuery();
                            String BackPlayer = null;
                            while(rs.next()){
                                BackPlayer = rs.getString("BackPlayer");
                            }
                            JDBCUtil.close(rs, null, connection, pst);
                            if (BackPlayer.equalsIgnoreCase("no")){
                                Value.map.put(player, null);
                                player.closeInventory();
                                player.sendMessage(ConfigManager.getSetting().getString("Message.SendInput"));
                            }else {
                                player.sendMessage(ConfigManager.getSetting().getString("Message.Already").replaceAll("%Player_Back%", BackPlayer));
                            }
                            break;
                        default:
                            a(player);
                            break;
                    }
                }
                if (ConfigManager.getSetting().getBoolean("Copy.enable")){
                    if (event.getCurrentItem().getItemMeta().getLore().contains(ConfigManager.getSetting().getString("Copy.Lore"))){
                        switch (ConfigManager.getSetting().getString("Storge.Type")){
                            case "Yaml":
                                b(player);
                                break;
                            case "MySql":
                                String sql1 = "select * from " + ConfigManager.getSetting().getString("Storge.SqlTablename") + " where PlayerName='"+ player.getName() +"'";
                                Connection conn = JDBCUtil.getConnection();
                                PreparedStatement pst = conn.prepareStatement(sql1);
                                ResultSet rs  = pst.executeQuery();
                                String meCode = null;
                                while(rs.next()){
                                    meCode = rs.getString("Code");
                                }
                                TextComponent text = new TextComponent();
                                text.setText(ConfigManager.getSetting().getString("Copy.SendCopyCode"));
                                text.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://" + meCode + "/"));
                                player.spigot().sendMessage(text);
                                player.closeInventory();
                                break;
                            default:
                                b(player);
                                break;
                        }
                    }
                }
            }
        }catch (Exception e){}
    }
    public void a(Player player){
        if (!ConfigManager.getConfig().getString(player.getName() + ".BackPlayer").equalsIgnoreCase("无")){
            player.sendMessage(ConfigManager.getSetting().getString("Message.Already").replaceAll("%Player_Back%", ConfigManager.getConfig().getString(player.getName() + ".BackPlayer")));
        }else {
            Value.map.put(player, null);
            player.closeInventory();
            player.sendMessage(ConfigManager.getSetting().getString("Message.SendInput"));
        }
    }
    public void b(Player player){
        TextComponent text = new TextComponent();
        text.setText(ConfigManager.getSetting().getString("Copy.SendCopyCode"));
        text.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://" + ConfigManager.getConfig().getString(player.getName() + ".Code") + "/"));
        player.spigot().sendMessage(text);
        player.closeInventory();
    }
}
